package com.sugon.controller.basics;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import com.sugon.annotation.AutoLog;
import com.sugon.annotation.NonBusinessParamValid;
import com.sugon.dao.ErmsFileDao;
import com.sugon.entity.AnnexFileEntity;
import com.sugon.entity.ErmsFileEntity;
import com.sugon.entity.KnowBaseEntity;
import com.sugon.entity.KnowFileEntity;
import com.sugon.entity.dto.ERMSFileStatDTO;
import com.sugon.entity.dto.IntoKnowParam;
import com.sugon.service.AnnexFileService;
import com.sugon.service.ErmsFileService;
import com.sugon.service.KnowBaseService;
import com.sugon.service.KnowFileService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * ERMS资料附件关系表,分为设备、模具、工装等资料Controller
 *
 * @author YuXD
 * @date 2021-01-11 10:05:02
 */
@Controller
@RequestMapping("ermsfile")
public class ErmsFileController {

    @Autowired
    private ErmsFileService ermsFileService;
    @Autowired
    private ErmsFileDao ermsFileDao;
    @Autowired
    private KnowBaseService knowBaseService;
    @Autowired
    private KnowFileService knowFileService;
    @Autowired
    private AnnexFileService annexFileService;

    /**
     * 查看列表
     */
    @RequestMapping("/moduleList")
    @ResponseBody
    public R listItem(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<ErmsFileEntity> annexFileList = ermsFileService.queryList(query);
        int total = ermsFileService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(annexFileList, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        String moduleId = MapUtil.getStr(params, "moduleId");
        Integer moduleType = MapUtil.getInt(params, "moduleType");
        List<ERMSFileStatDTO> list = ermsFileDao.statModuleFile(moduleId, moduleType);
        PageUtils pageUtil = new PageUtils(list, list.size(), query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 将ERMS模块的档案资料信息纳入知识库
     */
    @NonBusinessParamValid
    @RequestMapping("/saveKnow")
    @ResponseBody
    public R saveKnow(@RequestBody IntoKnowParam intoKnowParam, BindingResult bindingResult) {
        // 校验知识库是否存在
        KnowBaseEntity knowBase = knowBaseService.queryObject(intoKnowParam.getKnowbaseId());
        if (knowBase == null) {
            return R.error("知识库不存在，请确认！");
        }
        // 获取需要纳入知识库的档案资料列表
        List<ErmsFileEntity> ermsFileList = ermsFileService.queryList(intoKnowParam.getModuleId(), intoKnowParam.getModuleType(), intoKnowParam.getFileTypeId());
        if (CollectionUtil.isEmpty(ermsFileList)) {
            return R.ok();
        }
        // 保存知识库信息
        KnowFileEntity knowFile = new KnowFileEntity();
        knowFile.setKnowId(intoKnowParam.getKnowbaseId());
        knowFile.setType(intoKnowParam.getModuleType().toString());
        for (ErmsFileEntity ermsFile : ermsFileList) {
            // 重新保存一份附件,并以最新保存的附件作为知识库文件
            AnnexFileEntity annexFile = annexFileService.queryObject(ermsFile.getFileId());
            if(annexFile == null){
                continue;
            }
            if(annexFileService.save(annexFile) > 0){
                knowFile.setFileId(annexFile.getId());
                knowFileService.save(knowFile);
            }
        }
        return R.ok();
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        ErmsFileEntity ermsFile = ermsFileService.queryObject(id);

        return R.ok().put("ermsFile", ermsFile);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ResponseBody
    public R save(@RequestBody ErmsFileEntity ermsFile) {
        ermsFileService.save(ermsFile);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ResponseBody
    public R update(@RequestBody ErmsFileEntity ermsFile) {
        ermsFileService.update(ermsFile);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        ermsFileService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<ErmsFileEntity> list = ermsFileService.queryList(params);

        return R.ok().put("list", list);
    }
}
